package com.blaze.framework.core.util.uitls;

import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author created by Zhou Zongbo on 2025/5/29.
 */
public class ConvertUtil {
    private ConvertUtil() {

    }

    /**
     * object to boolean
     * @param obj obj
     * @return boolean
     */
    public static Boolean toBoolean(Object obj) {
        if (Objects.isNull(obj)) {
            return Boolean.FALSE;
        }

        if (obj instanceof Boolean) {
            return (Boolean) obj;
        }

        String str = toStr(obj);
        if (StringUtils.isBlank(str)) {
            return Boolean.FALSE;
        }

        return Boolean.parseBoolean(str);
    }


    /**
     * object to str
     * @param obj obj
     * @return str
     */
    public static String toStr(Object obj) {
        if (Objects.isNull(obj)) {
            return StringUtils.EMPTY;
        }

        if (obj instanceof String) {
            return (String) obj;
        }

        return obj.toString();
    }
}
